<?php
// +----------------------------------------------------------------------
// | sfadmin - Suifeng Administration
// +----------------------------------------------------------------------
// | Copyright (c) 2017-2020 http://suifeng.tech All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: suifeng <1787585212@qq.com>
// +----------------------------------------------------------------------

namespace app\admin\common;

use suifeng\helper\HtmlHelper;

class TableHelper
{
    public $array = array();

    public function __construct($array)
    {
        $this->array = $array;
    }

    public function renderTable($options)
    {
        $controller = isset($options['controller']) ? $options['controller'] : null;
        $form_url = isset($options['form_url']) ? $options['form_url'] : request()->url();
        $page_url = isset($options['page_url']) ? $options['page_url'] : $controller->url('index', array('page' => '[page]'));
        $key_field = isset($options['key_field']) ? $options['key_field'] : 'id';
        $selectable = isset($options['selectable']) ? $options['selectable'] : true;
        $columns = isset($options['columns']) ? $options['columns'] : array();
        $actions = isset($options['actions']) ? $options['actions'] : array();
        $pagination = isset($options['pagination']) ? $options['pagination'] : array();
        unset($options['controller'], $options['form_url'], $options['page_url'], $options['key_field'], $options['selectable'], $options['columns'], $options['actions'], $options['pagination']);

        $selected = isset($pagination['selected']) ? $pagination['selected'] : array();

        $form_attributes['method'] = 'get';
        $form_attributes['action'] = $form_url;
        $html = HtmlHelper::render_start_tag('form', $form_attributes);

        $table_attributes = $options;
        $table_attributes['class'] = 'table table-bordered table-striped table-hover';
        $html .= HtmlHelper::render_start_tag('table', $table_attributes);
        $html .= HtmlHelper::render_start_tag('thead');
        $html .= HtmlHelper::render_start_tag('tr');

        if ($selectable) {
            $html .= HtmlHelper::render_tag('th', array('class' => 'list-ids', 'width' => '30'), $this->renderHeaderSelect());
        }

        foreach ($columns as $field => $config) {
            if (isset($config['title'])) {
                $title = $config['title'];
                unset($config['title']);
            } else {
                $title = strtoupper($field);
            }

            if (isset($config['header'])) {
                $header_attributes = $config['header'];
                unset($config['header']);
            } else {
                $header_attributes = null;
            }

            $html .= HtmlHelper::render_tag('th', $header_attributes, $this->renderHeaderText($title));
        }

        if ($actions) {
            $html .= HtmlHelper::render_tag('th', null, $this->renderHeaderText('操作'));
        }

        $html .= HtmlHelper::render_end_tag('tr');
        $html .= HtmlHelper::render_end_tag('thead');
        $html .= HtmlHelper::render_start_tag('tbody');

        if ($this->array) {
            foreach ($this->array as $row) {
                $html .= HtmlHelper::render_start_tag('tr');

                if ($selectable) {
                    $html .= HtmlHelper::render_tag('td', null, $this->renderColumnSelect($row, $key_field, array('selected' => $selected)));
                }

                foreach ($columns as $field => $config) {
                    if (isset($config['column'])) {
                        $column_attributes = $config['column'];
                        unset($config['column']);
                    } else {
                        $column_attributes = array();
                    }

                    $html .= HtmlHelper::render_tag('td', $column_attributes, $this->renderColumnText($row, $field, $config));
                }

                if ($actions) {
                    $html .= HtmlHelper::render_start_tag('td', array('class' => 'actions'));

                    foreach ($actions as $action) {
                        if (isset($action['title'])) {
                            $text = $action['title'];
                            unset($action['title']);
                        } else {
                            $text = null;
                        }

                        if (isset($action['icon'])) {
                            $icon = $action['icon'];
                            unset($action['icon']);
                        } else {
                            $icon = null;
                        }

                        if (isset($action['class'])) {
                            $class = $action['class'];
                            unset($action['class']);
                        } else {
                            $class = null;
                        }

                        if ($action['url']) {
                            $key_value = $this->_getValue($row, $key_field);
                            $action['url'] = str_replace(urlencode('[id]'), $key_value, $action['url']);
                        }

                        $html .= $this->renderAction($text, $icon, $class, $action);
                    }

                    $html .= HtmlHelper::render_end_tag('td');
                }

                $html .= HtmlHelper::render_end_tag('tr');
            }
        } else {
            $html .= HtmlHelper::render_start_tag('tr', array('class' => 'data-empty'));
            $html .= HtmlHelper::render_start_tag('td', array('class' => 'text-center empty-info', 'colspan' => '50'));
            $html .= HtmlHelper::render_tag('i', array('class' => 'fa fa-database'));
            $html .= HtmlHelper::render_tag('span', null, '暂时没有数据');
            $html .= HtmlHelper::render_end_tag('td');
            $html .= HtmlHelper::render_end_tag('tr');
        }

        $html .= HtmlHelper::render_end_tag('tbody');
        $html .= HtmlHelper::render_end_tag('table');
        $html .= HtmlHelper::render_end_tag('form');

        if ($this->array && $pagination) {
            $page_object = new \suifeng\builder\Pagination();
            $page_object->totalRows = $pagination['rows_count'];
            $page_object->listRows = $pagination['page_size'];
            $page_object->nowPage = $pagination['page_num'];
            $page_object->url = $page_url;

            $html .= HtmlHelper::render_start_tag('div', array('class' => 'row'));

            $html .= HtmlHelper::render_start_tag('div', array('class' => 'col-sm-5'));
            $html .= HtmlHelper::render_start_tag('div', array('class' => 'pagination-info'));
            $html .= $page_object->show_info();
            $html .= HtmlHelper::render_end_tag('div');
            $html .= HtmlHelper::render_end_tag('div');

            $html .= HtmlHelper::render_start_tag('div', array('class' => 'col-sm-7'));
            $html .= HtmlHelper::render_start_tag('ul', array('class' => 'pagination'));
            $html .= $page_object->show_pages();
            $html .= HtmlHelper::render_end_tag('ul');
            $html .= HtmlHelper::render_end_tag('div');

            $html .= HtmlHelper::render_end_tag('div');
        }

        return $html;
    }

    public function renderHeaderSelect($options = array())
    {
        $attributes = $options;
        $attributes['type'] = 'checkbox';
        $attributes['class'] = 'check-all';
        return HtmlHelper::render_tag('input', $attributes);
    }

    public function renderHeaderText($text, $options = array())
    {
        if (empty($options)) {
            return $text;
        }

        $attributes = $options;
        return HtmlHelper::render_tag('span', $attributes, $text);
    }

    public function renderColumnText($row, $field, $options = array())
    {
        if (isset($options['list'])) {
            $list = $options['list'];
            unset($options['list']);
        } else {
            $list = array();
        }

        $value = $this->_getValue($row, $field);

        if (array_key_exists($value, $list)) {
            $display = $list[$value];

            if (is_array($display)) {
                $text = array_shift($display);
                $class = array_shift($display);

                $attributes = $options;
                $attributes['class'] = $class;
                $display = HtmlHelper::render_tag('span', $attributes, $text);
            }
        } else {
            $display = $value;
        }

        return $display;
    }

    public function renderColumnSelect($row, $field, $options = array())
    {
        if (isset($options['selected'])) {
            $selected = $options['selected'];
            unset($options['selected']);
        } else {
            $selected = array();
        }

        $value = $this->_getValue($row, $field);
        $checked = in_array($value, $selected);

        $attributes = $options;
        $attributes['type'] = 'checkbox';
        $attributes['class'] = 'ids';
        $attributes['name'] = 'ids[]';
        $attributes['value'] = $value;
        if ($checked) {
            $attributes['checked'] = 'checked';
        }
        return HtmlHelper::render_tag('input', $attributes);
    }

    private function _getValue($row, $field)
    {
        if (isset($row[$field])) {
            return $row[$field];
        } else {
            return null;
        }
    }

    public function renderAction($text, $icon, $class, $options = array())
    {
        $icon_html = $icon ? HtmlHelper::render_tag('i', array('class' => $icon)) : null;

        $attributes = $options;
        $attributes['type'] = 'button';
        $attributes['class'] = $class;
        $attributes['title'] = $text;
        return HtmlHelper::render_tag('button', $attributes, $icon_html);
    }
}